<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-form ref="queryForm" :inline="true" label-width="80px">
          <el-row>
            <el-form-item label="批次号">
              <el-input v-model="query.batchCode" placeholder="请输入测试批次号" />
            </el-form-item>
            <el-form-item label="数据日期">
              <el-input v-model="query.bizDate" placeholder="请输入数据日期" />
            </el-form-item>
            <el-form-item label="脚本名称">
              <el-input v-model="query.scriptName" placeholder="请输入脚本名称" />
            </el-form-item>
            <el-form-item label="测试状态">
              <el-select v-model="query.status" placeholder="请选择测试状态" clearable style="width:190px">
                <el-option v-for="item in statusList" :key="item.key" :label="item.label" :value="item.key" />
              </el-select>
            </el-form-item>
            <rrOperation :crud="crud" />
          </el-row>
        </el-form>
      </div>
    </div>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="crud.loading"
      max-height="320"
      :data="crud.data"
      row-key="pkId"
      border
      @filter-change="crud.handleFilterChange"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column prop="batchCode" label="批次号" />
      <el-table-column prop="bizDate" label="数据日期" />
      <el-table-column prop="scriptName" label="脚本名称" />
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 'SUCCESS'" type="success">成功</el-tag>
          <el-tag v-else-if="scope.row.status == 'FAIL'" type="warning">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="测试历史" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="showTestHistory(scope.row)">历史详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="showTestHistoryFlag"
      title="脚本测试历史详情"
      width="1200px"
      top="5vh"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="脚本测试" name="first" />
      </el-tabs>
      <div v-show="activeName==='first'">
        <test-history v-if="showTestHistoryFlag" :test-script-task-history-data="singleScriptTaskHistoryData" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CRUD, { presenter, header, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import testHistory from '../../components/testHistory'

const defaultCrud = CRUD({
  title: '脚本测试结果',
  url: '/api/script/data/test/result'
})
export default {
  components: { testHistory, pagination, rrOperation },
  mixins: [presenter(defaultCrud), header(), crud()],
  props: {
    testScriptTaskPkId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      statusList: [
        { key: 'SUCCESS', label: '成功' },
        { key: 'FAIL', label: '失败' }
      ],
      showTestHistoryFlag: false,
      scriptTestErrorMessage: null,
      showTestScriptTaskHistoryPkId: '',
      showTestScriptTaskListPkId: '',
      activeName: 'first',
      testScriptTaskHistoryData: [],
      singleScriptTaskHistoryData: []
    }
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.taskPkId = this.$props.testScriptTaskPkId
    },
    [CRUD.HOOK.afterRefresh](crud) {
      this.testScriptTaskHistoryData = crud.data
      const res = new Map()
      crud.data = crud.data.filter((a) => !res.has(a.scriptName) && res.set(a.scriptName, 1))
      crud.page.total = crud.data.length
    },
    // 展示测试历史详情
    showTestHistory(data) {
      this.singleScriptTaskHistoryData = this.testScriptTaskHistoryData.filter((item) => data.scriptName === item.scriptName)
      this.showTestHistoryFlag = true
      this.showTestScriptTaskHistoryPkId = data.taskPkId
      this.showTestScriptTaskListPkId = data.scriptTestListPkId
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
